<?php

declare(strict_types=1);

namespace Vipkwd\SDK\Qrcdr;

use Vipkwd\SDK\Qrcdr\Dependents\Data;
use Vipkwd\SDK\Qrcdr\Dependents\Qrcdr_Core;

/**
    $appId = '2aefc95020c5ad10';
    $appKey = 'b9331264ab478705b3f728d5';
    $api = 'https://qrcode.demo.com/v1/api/generator';
    $sdk = new Qrcdr($appId, $appKey, $api);

    $data = $sdk->text($qrcodeData['text']);
 */
class Qrcdr
{
    use Qrcdr_Core;
    public function __construct(string $appId, string $appKey, string $api = '')
    {
        if ($api)
            $this->_api = $api;

        $this->options = array_merge([
            'api' => $this->_api,
            'appId' => $appId,
            'appKey' => $appKey,

            // 'scheme' => '',
            // 'host' => '',
            // 'path' => '',
        ], parse_url($this->_api));

        if (!self::$sdk)
            self::$sdk = $this;
    }

    /**
     * 生成网址二维码并输出到浏览器
     * 
     * @param string $url
     * @param array $params
     * 
     * @return array
     */
    public function url(string $url, array $params = [])
    {
        $response = $this->send('link', $params, ['link' => $url]);
        unset($data, $params);
        return $response;
    }

    /**
     * 生成文本二维码并输出到浏览器
     * 
     * @param string $text
     * @param array $params
     * 
     * @return array
     */
    public function text(string $text, array $params = [])
    {
        $response = $this->send('text', $params, ['data' => trim($text)]);
        unset($params, $text);
        return $response;
    }

    /**
     * Summary of wifi
     * @param array $data[ssid, pass, network, hide]
     * @param array $params
     * @return array
     */
    public function wifi(array $data, array $params = [])
    {
        $data = Data::instance($data);
        $response = $this->send('wifi', $params, [
            'wifissid' => $data->get('ssid'),
            'wifipass' => $data->get('pass'),
            'networktype' => $data->get('network'),
            'wifihidden' => $data->get('hide'),
        ]);
        unset($data, $params);
        return $response;
    }

    /**
     * Summary of email
     * @param array $data[to, subject, body]
     * @param array $params
     * @return array
     */
    public function email(array $data, array $params = [])
    {
        $data = Data::instance($data);
        $response = $this->send('email', $params, [
            'to' => $data->get('to'),
            'subject' => $data->get('subject'),
            'body' => $data->get('body', 'Mail send by qrcdr.'),
        ]);
        unset($data, $params);
        return $response;
    }

    /**
     * Summary of tel
     * @param array $data[countrycode, tel]
     * @param array $params
     * @return array
     */
    public function tel(array $data, array $params = [])
    {
        $data = Data::instance($data);
        $response = $this->send('tel', $params, [
            'countrycode' => $data->get('countrycode'),
            'tel' => $data->get('tel'),
        ]);
        unset($data, $params);
        return $response;
    }

    /**
     * Summary of sms
     * @param array $data[countrycode, number, body]
     * @param array $params
     * @return array
     */
    public function sms(array $data, array $params = [])
    {
        $data = Data::instance($data);
        $response = $this->send('sms', $params, [
            'countrycode' => $data->get('countrycode'),
            'number' => $data->get('number'),
            'body' => $data->get('body'),
        ]);
        unset($data, $params);
        return $response;
    }
    /**
     * Summary of whatsapp
     * @param array $data[countrycode, number, message]
     * @param array $params
     * @return array
     */
    public function whatsapp(array $data, array $params = [])
    {
        $data = Data::instance($data);
        $response = $this->send('whatsapp', $params, [
            'countrycode' => $data->get('countrycode'),
            'number' => $data->get('number'),
            'message' => $data->get('message'),
        ]);
        unset($data, $params);
        return $response;
    }

    /**
     * Summary of location
     * @param array $data[lat, lng]
     * @param array $params
     * @return array
     */
    public function location(array $data, array $params = [])
    {
        $data = Data::instance($data);
        $response = $this->send('location', $params, [
            'lat' => $data->get('lat'),
            'lng' => $data->get('lng')
        ]);
        unset($data, $params);
        return $response;
    }
    /**
     * Summary of bitcoin
     * @param array $data[account, amount, label, message]
     * @param array $params
     * @return array
     */
    public function bitcoin(array $data, array $params = [])
    {
        $data = Data::instance($data);
        $response = $this->send('bitcoin', $params, [
            'account' => $data->get('account'),
            'amount' => $data->get('amount'),
            'label' => $data->get('label'),
            'message' => $data->get('message'),
        ]);
        unset($data, $params);
        return $response;
    }
    
}